package com.android.hedgehog;

import com.scoreloop.client.android.core.controller.TermsOfServiceController;
import com.scoreloop.client.android.core.controller.TermsOfServiceControllerObserver;
import com.scoreloop.client.android.core.model.Session;
import com.scoreloop.client.android.core.model.TermsOfService;
import com.scoreloop.client.android.core.model.TermsOfService.Status;
import com.scoreloop.client.android.core.model.User;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class GlobalRank extends Activity implements View.OnClickListener, ScoreInterface {
	TermsOfService tos = Session.getCurrentSession().getUsersTermsOfService();
	TermsOfServiceControllerObserver observer;
	TermsOfServiceController controller;
	Intent intent;
	ScoreManager sm;
	// Typeface type = Typeface.createFromAsset(getAssets(), "smallfont.ttf");

	Button getRank, checkRank;
	TextView rankDisplay;

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.globalrank);

		getRank = (Button) findViewById(R.id.bGetRank);
		getRank.setOnClickListener(this);
		checkRank = (Button) findViewById(R.id.bCheckRank);
		checkRank.setOnClickListener(this);
		rankDisplay = (TextView) findViewById(R.id.tvRankDisplay);
		rankDisplay.setTextSize(22);
		rankDisplay.setText("????");
	}

	@Override
	public void onClick(final View v) {

		Log.d("----------", "11111111111111");

		if (tos.getStatus() == Status.ACCEPTED) {// ACCEPTED
			Log.d("----------", "222222222222AAAAAAAAAAAAA");
			activateButton(v);

		}// /////////ACCEPTED

		else {// ////////////////NOT ACCEPTED
			Log.d("----------", "2222222222BBBBBBBBBBBBBBBBB");
			observer = new TermsOfServiceControllerObserver() {// CONTROLLEROBSERVER

				@Override
				public void termsOfServiceControllerDidFinish(TermsOfServiceController toscontroller,
						Boolean accepted) {

					try {// //////TRY&CATCH
						Log.d("----------", "55555555555555555555555");

						if (accepted) {
							activateButton(v);
						} else {
							Toast.makeText(getApplicationContext(), "must accept terms to check rankings",
									Toast.LENGTH_LONG).show();
						}

					} catch (NullPointerException e) {
						Toast.makeText(getApplicationContext(), "No Internet Connection", Toast.LENGTH_LONG)
								.show();

					}// /////////TRY&CATCH
				}

			};// CONTROLLEROBSERVER
			Log.d("----------", "3333333333333333333");
			controller = new TermsOfServiceController(observer);
			controller.query(getParent());
			Log.d("----------", "4444444444444444444");
		}// ////////////////////NOT ACCEPTED

	}// ONCLICK

	private void activateButton(View v) {

		switch (v.getId()) {// *****SWITCH
		case R.id.bGetRank:
			intent = new Intent(GlobalRank.this, SubmitScore.class);
			startActivity(intent);
			// sm = new ScoreManager(this);
			// sm.uploadScore(43);
			break;
		case R.id.bCheckRank:
			intent = new Intent(GlobalRank.this, LeaderBoard.class);
			startActivity(intent);
			break;
		}// **********************SWITCH

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		rankDisplay.setText("-- " + resultCode);
	}

	@Override
	public void setGlobalRank(int rank) {
		rankDisplay.setTextSize(30);
		// rankDisplay.setTypeface(type);
		rankDisplay.setText("" + rank);
		Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_SHORT).show();
	}

}
